using System;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Betfair.Api
{
    internal sealed class ProxyBuilder
    {
        private readonly string _applicationKey;
        private readonly string _url;

        public ProxyBuilder(string url, string applicationKey)
        {
            _url = url;
            _applicationKey = applicationKey;
        }

        public async Task<BetfairResponse<T>> Build<T>(string sessionToken, string method, Params parameters, int id)
        {
            try
            {
                return
                    await
                        new WebHelper().Seturl(_url)
                            .SetSessionToken(sessionToken)
                            .SetApplicationKey(_applicationKey)
                            .SetDataToPost(JsonConvert.SerializeObject(new BetfairRequest(method, parameters, id)))
                            .Build<BetfairResponse<T>>();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}